<template>
   <div>
    <MyHeader title="购物车案例"></MyHeader>
    <div class="main">
      <MyGoods :list="list" 
      @downFn="downFn"
      @upFn="upFn"
      ></MyGoods>
    </div>
    <MyFooter :list="list" @checkedAll="checkedAll"></MyFooter>
  </div>
</template>

<script>
import MyHeader from './components/MyHeader'
import MyGoods from './components/MyGoods'
import MyFooter from './components/MyFooter'
import axios from "axios"
export default {
  components: {
    MyHeader,
    MyGoods,
    MyFooter
  },
  data (){
    return {
    list:[]
    }
  },
   async created (){
    const { data } = await axios.get('https://www.escook.cn/api/cart')
    // console.log(data);
    this.list = data.list
  },
  methods:{
    downFn(id){
      const obj = this.list.find(item=>item.id === id)
      if(obj.goods_count > 0){
        obj.goods_count = obj.goods_count - 1
      }
    },
    upFn(id){
      const obj = this.list.find(item=>item.id === id)
      obj.goods_count = obj.goods_count + 1
    },
    checkedAll(val){//全选反选
      this.list.forEach(item=>{
        item.goods_state = val
      })
    }
  }
}
</script>

<style>
  .main{
    padding-top: 45px;
    padding-bottom: 50px;
  }
</style>